package com.andy.module.ext

import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.andy.module.R
import com.andy.module.widget.CustomToolBar

/**
 * @creator:LuoFan
 * @date:2022/5/31
 * @description:
 */
fun LinearLayout.addToolBar(
    title: String = "",
    backImg: Int = R.mipmap.icon_back,
    onBack: (view: View) -> Unit
): CustomToolBar {
    val titleBarView = LayoutInflater.from(context).inflate(R.layout.layout_titlebar_view, null)
    val mCustomToolbar = titleBarView.findViewById<CustomToolBar>(R.id.customToolBar)
    mCustomToolbar.setCenterTitle(title)
    if (backImg == 0) {
        mCustomToolbar.getBaseToolBar().navigationIcon = null
    } else {
        mCustomToolbar.getBaseToolBar().setNavigationIcon(backImg)
    }
    mCustomToolbar.getBaseToolBar().setNavigationOnClickListener {
        onBack.invoke(this)
    }
    addView(titleBarView, 0)
    return mCustomToolbar
}

fun CustomToolBar.initExt(
    title: String = "",
    backImg: Int = R.mipmap.icon_back,
    onBack: (view: View) -> Unit
) {
    setCenterTitle(title)
    if (backImg == 0) {
        getBaseToolBar().navigationIcon = null
    } else {
        getBaseToolBar().setNavigationIcon(backImg)
    }
    getBaseToolBar().setNavigationOnClickListener {
        onBack.invoke(this)
    }
}